package com.juwang.girl.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import com.qhad.ads.activity.SampleBannerActivity;
import com.qhad.ads.sdk.adcore.Qhad;
import com.qhad.ads.sdk.interfaces.IQhAdEventListener;
import com.qhad.ads.sdk.interfaces.IQhBannerAd;
import com.qhad.ads.sdk.interfaces.IQhInterstitialAd;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class QihuAdvertis {
	public static String bannerid = "uauGaUop3a";
	public static String floatBannerid = "55uGaf9UsG";
	public static String interid = "5Fkb50BU1a";
	
	public static IQhInterstitialAd interstitialAd;
	
	public static void loadQihuAdBanner(final Context context, final RelativeLayout relativeLayout){
		final String urlStr = QihuAdvertis.getUrl(context);
		Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(3000);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        InputStream is = conn.getInputStream();
                        byte[] buffer = new byte[2048];
                        int readBytes = 0;
                        StringBuilder stringBuilder = new StringBuilder();
                        while((readBytes = is.read(buffer)) > 0){
                            stringBuilder.append(new String(buffer, 0, readBytes));
                        }
                        String result = stringBuilder.toString();
                        JSONObject obj = new JSONObject(result);
                       
                        if(obj!=null && obj.getInt("status")==1){
                        	((Activity)context).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								IQhBannerAd bannerad = Qhad.showBanner(relativeLayout, (Activity)context, bannerid, false); // 请求广告
     							}
                        	});
                        }else{
                        	 ((Activity)context).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								// TODO Auto-generated method stub
     								Toast.makeText(context, "广告已过期，请与供应商联系~", Toast.LENGTH_SHORT).show();
     							}
     						});
                        }
                       
                        
                        is.close();
                        conn.disconnect();
                    }else{
                    	conn.disconnect();
                    	((Activity)context).runOnUiThread(new Runnable() {
 							
 							@Override
 							public void run() {
 								// TODO Auto-generated method stub
 								Toast.makeText(context, "请检查网络连接", Toast.LENGTH_SHORT).show();
 							}
 						});
                    	
                    }
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
		});
		thread.start();
		
	}
	
	public static void loadQihuInterAd(final Context context) {
		
		final String urlStr = QihuAdvertis.getUrl(context);
		Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(3000);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        InputStream is = conn.getInputStream();
                        byte[] buffer = new byte[2048];
                        int readBytes = 0;
                        StringBuilder stringBuilder = new StringBuilder();
                        while((readBytes = is.read(buffer)) > 0){
                            stringBuilder.append(new String(buffer, 0, readBytes));
                        }
                        String result = stringBuilder.toString();
                        JSONObject obj = new JSONObject(result);
                       
                        if(obj!=null && obj.getInt("status")==1){
                        	((Activity)context).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								interstitialAd = Qhad.showInterstitial((Activity)context, interid, false);
     							}
                        	});
                        }else{
                        	 ((Activity)context).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								// TODO Auto-generated method stub
     								Toast.makeText(context, "广告已过期，请与供应商联系~", Toast.LENGTH_SHORT).show();
     							}
     						});
                        }
                       
                        
                        is.close();
                        conn.disconnect();
                    }else{
                    	conn.disconnect();
                    	((Activity)context).runOnUiThread(new Runnable() {
 							
 							@Override
 							public void run() {
 								// TODO Auto-generated method stub
 								Toast.makeText(context, "请检查网络连接", Toast.LENGTH_SHORT).show();
 							}
 						});
                    	
                    }
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
		});
		thread.start();

	}
	
	public static void showAd(final Activity activity) {
		final String urlStr = QihuAdvertis.getUrl(activity);
		Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(3000);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        InputStream is = conn.getInputStream();
                        byte[] buffer = new byte[2048];
                        int readBytes = 0;
                        StringBuilder stringBuilder = new StringBuilder();
                        while((readBytes = is.read(buffer)) > 0){
                            stringBuilder.append(new String(buffer, 0, readBytes));
                        }
                        String result = stringBuilder.toString();
                        JSONObject obj = new JSONObject(result);
                       
                        if(obj!=null && obj.getInt("status")==1){
                        	(activity).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								if(interstitialAd == null){
     									loadQihuInterAd((Context)activity);
     								}
     								interstitialAd.showAds(activity);
     							}
                        	});
                        }else{
                        	 (activity).runOnUiThread(new Runnable() {
     							
     							@Override
     							public void run() {
     								// TODO Auto-generated method stub
     								Toast.makeText(activity, "广告已过期，请与供应商联系~", Toast.LENGTH_SHORT).show();
     							}
     						});
                        }
                       
                        
                        is.close();
                        conn.disconnect();
                    }else{
                    	conn.disconnect();
                    	(activity).runOnUiThread(new Runnable() {
 							
 							@Override
 							public void run() {
 								// TODO Auto-generated method stub
 								Toast.makeText(activity, "请检查网络连接", Toast.LENGTH_SHORT).show();
 							}
 						});
                    	
                    }
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
		});
		thread.start();
		
	}
	
	
	public static String getUrl(Context context){
		String packageName = context.getPackageName();
		String url = "http://120.55.96.1/ads_manage/index.php?m=&c=Api&a=check&package="+packageName;
		return url;
	}
}
